package com.smart.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.global.GlobalWord;
import com.smart.pojo.LeaveBill;
import com.smart.mapper.LeaveBillMapper;
import com.smart.resp.LeaveBillResp;
import com.smart.service.ILeaveBillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smart.service.IProcessService;
import com.smart.utils.CopyUtil;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lyj
 * @since 2023-05-02
 */
@Service
public class LeaveBillServiceImpl extends ServiceImpl<LeaveBillMapper, LeaveBill> implements ILeaveBillService {

    @Autowired
    TaskService taskService;

    @Override
    public LeaveBillResp queryByBillId(long billId) {
        return baseMapper.queryByBillId(billId);
    }

    @Override
    public void queryPageByUserId(Long userId, Page page) {
        this.lambdaQuery().eq(LeaveBill::getUserId, userId).page(page);
        List<LeaveBillResp> leaveBillResps = CopyUtil.copyList(page.getRecords(), LeaveBillResp.class);
        for (LeaveBillResp leaveBillResp : leaveBillResps) {
            String businessKey = GlobalWord.LEAVE_KEY + "." + leaveBillResp.getId();
            Task task = taskService.createTaskQuery().processInstanceBusinessKey(businessKey).singleResult();

            leaveBillResp.setTaskName(task == null ? "已结束" : task.getName());

        }
        page.setRecords(leaveBillResps);
    }
}
